package com.lanou.lenanapp.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.CollectImageAdapter;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.CollectImageEntity;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class ImageCollectFragment extends Fragment implements CollectImageAdapter.OnRemoveImageClickListener {

    private RecyclerView recyclerView;
    private GridLayoutManager manager;
    private DatabaseHelper helper;
    private Context context;
    private CollectImageAdapter adapter;
    private TextView goneTv;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.fragment_collect_image, null);
        recyclerView = (RecyclerView) v.findViewById(R.id.imagerecycleeview);
        goneTv = (TextView) v.findViewById(R.id.goneTv);
        goneTv.setSelected(true);
        goneTv.requestFocus();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        helper = DatabaseHelper.getHelper();
        manager = new GridLayoutManager(getActivity(), 1);
        adapter = new CollectImageAdapter(getActivity());
        adapter.setOnRemoveImageClickListener(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        //查询收藏图片的数据库
        List<CollectImageEntity> list = helper.queryAllImageCollect();
        if (list != null && list.size() > 0){
            adapter.addData(list);//将数据添加到适配器
        }else {//如果数据空的就显示图片
            goneTv.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void OnRemoveImageClick(List<CollectImageEntity> list) {
        if (list == null || list.size() == 0){
            goneTv.setVisibility(View.VISIBLE);
        }
    }
}
